const moment = require('moment');
module.exports = app => {
  const {STRING, DATE, BIGINT, BOOLEAN} = app.Sequelize;
  const ArticleType = app.model.define('articleType', {
    id: {
      type: BIGINT,
      primaryKey: true,
      autoIncrement: true
    },
    name: STRING(100),
    parent_id: BIGINT,
    grand_parent_id: BIGINT,
    create_time: {
      type: DATE,
      get() {
        // .utcOffset(0)
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    img: STRING(100),
    publish: BOOLEAN
  }, {
    tableName: 'hy_article_type',
    timestamps: false,
  });
  
  ArticleType.associate = function() {
    app.model.ArticleType.hasMany(app.model.ArticleType, { as: 'children', foreignKey: 'parent_id', targetKey: 'id' });
    app.model.ArticleType.hasMany(app.model.Article, { as: 'articles', foreignKey: 'type', targetKey: 'id' });
    app.model.ArticleType.belongsTo(app.model.ArticleType, { as: 'parent', foreignKey: 'parent_id', targetKey: 'id' });
    app.model.ArticleType.belongsTo(app.model.ArticleType, { as: 'grandParent', foreignKey: 'grand_parent_id', targetKey: 'id' });
  };

  return ArticleType;
};
